package com.atguigu.srb.core.controller.api;


import com.atguigu.guigu.common.result.R;
import com.atguigu.service.base.utils.JwtUtils;
import com.atguigu.srb.core.pojo.entity.Lend;
import com.atguigu.srb.core.pojo.entity.vo.LendIndexSearchVO;
import com.atguigu.srb.core.service.LendService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的准备表 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2022-11-19
 */
@RestController
@RequestMapping("/api/core/lend")
public class ApiLendController {
    @Resource
    LendService lendService;
    @ApiOperation("查询登录用户是否为标的的借款人")
    @GetMapping("/auth/isBorrower/{id}")
    public R isBorrower(@PathVariable("id")String id,
                        @RequestHeader(value = "token" ,required = false)String token){
        Long userId = JwtUtils.getUserId(token);
        int count = lendService.count(Wrappers.lambdaQuery(Lend.class)
                .eq(Lend::getId, id)
                .eq(Lend::getUserId, userId));
        return R.ok().data("isBorrower" , count>0);
    }


    @ApiOperation("查询标的详情和标的的借款人数据")
    @GetMapping("{id}")
    public R getById(@PathVariable("id")String id){
        Map<String, Object> map = lendService.getLend(id);
        return R.ok().data(map);
    }
    @ApiOperation("带条件查询标的列表")
    @GetMapping ///api/core/lend?lendType=1,2,3&lendYearRate=1
    public R list(LendIndexSearchVO searchVO){
        List<Lend> lends = lendService.getLendsByCondition(searchVO);
        return R.ok().data("items" , lends);
    }
}

